package com.lab.servlet;




import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lab.dao.CollegeDao;
import com.lab.dao.CourseDao;
import com.lab.dao.MajorDao;
import com.lab.dao.StudentDao;
import com.lab.dao.impl.CollegeDaoImpl;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.dao.impl.StudentDaoImpl;
import com.lab.po.College;
import com.lab.po.Course;
import com.lab.po.Major;
import com.lab.po.PageBean;
import com.lab.po.Student;

//控制层-查询全体学生业务逻辑
@WebServlet("/showAllServlet")
public class ShowAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = (String) request.getParameter("method");
		if ("showStudent".equals(method)) {
			showStudent(request, response);
		}else if ("showColMaj".equals(method)) {
			showColMaj(request, response);
		}else if ("showCourse".equals(method)) {
			showCourse(request, response);
		}else if ("showPerson".equals(method)) {
			showPerson(request, response);
		}
		
	}


	private void showPerson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		int id =Integer.parseInt(request.getSession().getAttribute("stId").toString());
		StudentDao studao = new StudentDaoImpl();
		Student student = studao.findStudentByid(id);
		request.setAttribute("student", student);
		String mainPage = "personStudent.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("stumain.jsp").forward(request, response);
		
	}


	private void showCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int currentPage=1;
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		CourseDao cd=new CourseDaoImpl();
		PageBean pb=new PageBean();
		pb.setCurrentPage(currentPage);
		pb.setCountRecords(cd.countCourse());
		Map map=new HashMap();
		map.put("key1", (currentPage-1)*pb.getCountPage());
		map.put("key2", pb.getCountPage());
		List<Course> courseList=cd.queryCourse(map);
		int status=cd.status();
		if(request.getSession().getAttribute("stId")!=null){
			String mainPage="chooseCourses.jsp";
			request.setAttribute("status", status);
			if(status==1){
				request.setAttribute("PageBean", pb);
				request.setAttribute("courseList", courseList);
			}
			request.setAttribute("mainPage", mainPage);
			request.getRequestDispatcher("stumain.jsp").forward(request, response);
		}else{
			String mainPage="showCourse.jsp";
			request.setAttribute("status", status);
			request.setAttribute("PageBean", pb);
			request.setAttribute("courseList", courseList);
			request.setAttribute("mainPage", mainPage);
			request.getRequestDispatcher("main.jsp").forward(request, response);
		}
	}


	private void showColMaj(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		CollegeDao collegedao=new CollegeDaoImpl();
		List<College> collegeList=collegedao.queryCollege();
		String mainPage="showCollegeMajor.jsp";
		request.setAttribute("collegeList", collegeList);
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}


	private void showStudent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int currentPage=1;
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		StudentDao studao = new StudentDaoImpl();
		PageBean pb=new PageBean();
		pb.setCurrentPage(currentPage);
		pb.setCountRecords(studao.countStudent());
		List<Student> list = studao.StSelect(currentPage);
		String mainPage="displaystudent.jsp";
		request.setAttribute("PageBean", pb);
		request.setAttribute("list", list);
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}

}
